import HTTP_CODE from '../constant/http-code'
import * as request from '../constant/fetch-request'
import message from 'antd/es/message'
import {convertContent} from '../constant/lang-util'

export const refreshMyNotes = myNotes => ({
  type: 'REFRESH_MY_NOTES',
  myNotes
})

export const getMyNotes = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`../api/v2/myNotes/${programId}`)
      if (res.status === HTTP_CODE.CREATED) {
        dispatch(refreshMyNotes(res.body))
      }
    })()
  }
}

export const deleteNote = (noteId, programId) => {
  return dispatch => {
    (async () => {
      const res = await request.del(`../api/v2/myNotes/${noteId}`)
      if (res.status === HTTP_CODE.NO_CONTENT) {
        message.success(convertContent('删除成功'))
        dispatch(getMyNotes(programId))
      }
    })()
  }
}
